package com.itheima.reggic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggic.common.BaseContext;
import com.itheima.reggic.common.R;
import com.itheima.reggic.dto.DishDto;
import com.itheima.reggic.pojo.*;
import com.itheima.reggic.service.OrderDetailService;
import com.itheima.reggic.service.OrdersService;
import com.itheima.reggic.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 下单功能
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("保存订单信息{}", orders);
        if (orders == null || orders.getAddressBookId() == null)
            return R.error("下单失败!");
        ordersService.submit(orders);
        return R.success("下单成功!");
    }

    @GetMapping("/userPage")
    public R<Page> page(Integer page, Integer pageSize) {
        Page<Dish> pageObj = new Page<>(page, pageSize);
        Page<Orders> pageDto = new Page<>(page, pageSize);
        Long uid = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,uid);
        List<Orders> list = ordersService.list(queryWrapper);
        pageDto.setRecords(list);
        return R.success(pageDto);

    }
}
